﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace ERP_RORZE_PERMISSION.Base
{
    public class LogUtils
    {
        private Permission permission;
        private string username = string.Empty;
        private string table = string.Empty;
        private string task = string.Empty;
        private string des = string.Empty;

        public LogUtils()
        {
            permission = new Permission();
            permission.Is_Login();
            this.username = permission.UserName;
        }

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public string Table
        {
            get { return table; }
            set { table = value; }
        }
        public string Task
        {
            get { return task; }
            set { task = value; }
        }
        public string Description
        {
            get { return des; }
            set { des = value; }
        }

        public void WriteLog()
        {
            string insertString = "INSERT INTO [222_Log]" +
                        "([Username],[Table],[Task],[Description],[Time]) " +
                    "Values(@Username, @Table, @Task, @Description, @Time) ";
            try
            {
                var paramArray = new SqlParameter[] {
					BaseDAO.CreateParameter("@Username",SqlDbType.NVarChar,(this.Username.Trim().Equals(string.Empty)) ? "": this.Username),
					BaseDAO.CreateParameter("@Table",SqlDbType.NVarChar, (this.table.Trim().Equals(string.Empty)) ? "": this.table),
					BaseDAO.CreateParameter("@Task",SqlDbType.NVarChar, (this.task.Trim().Equals(string.Empty)) ? "": this.task),
					BaseDAO.CreateParameter("@Description",SqlDbType.NVarChar, (this.des.Trim().Equals(string.Empty)) ? "": this.des),
					BaseDAO.CreateParameter("@Time",SqlDbType.DateTime, DateTime.Now),
				};

                BaseDAO.ExecuteNonQuery(insertString, paramArray);
            }
            catch (Exception ex)
            {
                throw new Exception("Log::Error", ex);
            }
        }
    }
}
